package com.cjm.mhl.service;

import com.cjm.mhl.dao.BillDAO;
import com.cjm.mhl.dao.MulitiDao;
import com.cjm.mhl.domain.Bill;
import com.cjm.mhl.domain.MulitiTableBenn;

import java.util.List;
import java.util.UUID;

public class BillSerivce {
    private BillDAO billDAO=new BillDAO();
    private MenuService menuService=new MenuService();
    private DiningTableService diningTableService=new DiningTableService();
    private MulitiDao mulitiDao=new MulitiDao();
    //编写点餐方法
    public boolean orderMenu(int menuId,int nums,int diningTableId){
      //生成一个账单单号
        String uuid = UUID.randomUUID().toString();
//        生成账单表
        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,now(),'未结账')", uuid, menuId, nums, menuService.getMenuById(menuId).getPrice() * nums, diningTableId);
        if (update<=0){
            return false;
        }
//        更新餐桌状态
         return diningTableService.updateDiningTableState(diningTableId,"就餐中");
    }
//    返回账单
    public List<Bill> list(){
        return billDAO.queryMulti("select * from bill",Bill.class);
    }

    public List<MulitiTableBenn> list2(){
        return mulitiDao.queryMulti("select bill.*,NAME "+" from bill ,menu "+"where bill.menuId=menu.id", MulitiTableBenn.class);

    }


//    查找某张餐桌是否未结账
    public boolean hasPayBiningTableId(int diningTableId){
        Bill bill = billDAO.querySingle("select * from bill where diningTableId=? and state='未结账' limit 0 , 1", Bill.class, diningTableId);
        return bill!=null;
    }
//    完成结账
    public boolean payBill(int diningTableId,String payMode){
//        修改bill
        int update = billDAO.update("update bill set state=? where diningTableId=? and state='未结账'", payMode, diningTableId);
        if (update<=0){
            return false;
        }
        if (!diningTableService.updateDiningTableFree(diningTableId,"空")){
            return false;
        }
        return true;
    }


}
